<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace suifeng\builder\form\control;

use suifeng\builder\form\Control;
use suifeng\helper\HtmlHelper;

class Radio extends Control
{
    /**
     * 选项列表。
     *
     * @var unknown
     */
    public $options;

    /**
     * 输出HTML。
     *
     * @return string
     */
    protected function _render_core()
    {
        $options = Select::parse_options($this->options);

        $html = '';

        foreach ($options as $value => $text) {
            $html .= $this->_render_radio_item($value, $text);
        }

        return $html;
    }

    protected function _render_radio_item($value, $text)
    {
        $attributes['name'] = $this->name;
        $attributes['type'] = 'radio';
        $attributes['class'] = 'radio';
        $attributes['value'] = $value;

        if ($value == $this->submitted_value) {
            $attributes['checked'] = 'checked';
        }

        $input = HtmlHelper::render_tag('input', $attributes);

        return <<<EOF
<div class="radio-inline lyui-control lyui-radio">
    <label>
        {$input}
        <span class="lyui-control-indicator"></span>
        <span>{$text}</span>
    </label>
</div>
EOF;
    }
}

?>
